package com.probiz.estore.product.service.impl;

import java.util.List;

import com.probiz.estore.common.model.product.Brand;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.product.dao.BrandDao;
import com.probiz.estore.product.service.BrandManager;


/**
 * Manager implementation for Brand, responsible for business processing, and communicate between web and persistence layer.
 */
public class BrandManagerImpl extends GenericManagerImpl<Brand> implements BrandManager {

	private BrandDao brandDao = null;

	/**
	 * @param brandDao
	 *            the brandDao to set
	 */
	public void setBrandDao(BrandDao brandDao) {
		this.brandDao = brandDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = brandDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(Brand entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
	 */
	@Override
	protected void preSave(Brand entity) {

	}

	public List<Brand> findAllBrands() {
		return listAllOrdered("sortOrder",true);
	}

	public Brand getBrandByCode(String brandCode) {
		Brand brand=dao.getUniqueByProperty("brandCode", brandCode);
		return brand;
	}

	public List<Brand> findBrandByName(String brandName) {
		return dao.listByProperty("brandName", brandName);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.BrandManager#getBrandIdByUri(java.lang.String)
	 */
    public Integer getBrandIdByUri(String uri){
    	String brandId = uri.substring(uri.lastIndexOf("cat"), uri
                .lastIndexOf("."));
        brandId = brandId.substring("cat".length());
        return Integer.valueOf(brandId);
    }

    /*
     * (non-Javadoc)
     * @see com.probiz.estore.product.service.BrandManager#getUriByBrandId(java.lang.Integer)
     */
    public String getUriByBrandId(Integer brandId){
    	 StringBuffer url = new StringBuffer();
         url.append("/brand");
         url.append("/cat");
         url.append(brandId.toString());
         url.append(".html");
         return url.toString();
    }
    public boolean getIsBrandExist(String brandName){
    	return this.brandDao.isBrandExist(brandName);
    }
    
    public boolean getIsBrandNameUseByOthers(String brandName, Integer brandId){
    	return this.brandDao.isBrandNameUseByOthers(brandName, brandId);
    }
}
